/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author
 */
public class BaiThi {
    //Danh sach thuoc tinh

    private int maBaiThi;
    private String deBai;
    private String dapAn1;
    private String dapAn2;
    private String dapAn3;
    private String dapAn4;
    private int dapAnDung;
    private int maDeThi;
    private int dapAnDaChon;

    //Danh sach cac ham get va set
    public int getMaBaiThi() {
        return maBaiThi;
    }

    public void setMaBaiThi(int maBaiThi) {
        this.maBaiThi = maBaiThi;
    }

    public String getDeBai() {
        return deBai;
    }

    public void setDeBai(String deBai) {
        this.deBai = deBai;
    }

    public String getDapAn1() {
        return dapAn1;
    }

    public void setDapAn1(String dapAn1) {
        this.dapAn1 = dapAn1;
    }

    public String getDapAn2() {
        return dapAn2;
    }

    public void setDapAn2(String dapAn2) {
        this.dapAn2 = dapAn2;
    }

    public String getDapAn3() {
        return dapAn3;
    }

    public void setDapAn3(String dapAn3) {
        this.dapAn3 = dapAn3;
    }

    public String getDapAn4() {
        return dapAn4;
    }

    public void setDapAn4(String dapAn4) {
        this.dapAn4 = dapAn4;
    }

    public int getDapAnDung() {
        return dapAnDung;
    }

    public void setDapAnDung(int dapAnDung) {
        this.dapAnDung = dapAnDung;
    }

    public int getDapAnDaChon() {
        return dapAnDaChon;
    }

    public void setDapAnDaChon(int dapAnDaChon) {
        this.dapAnDaChon = dapAnDaChon;
    }

    public int getMaDeThi() {
        return maDeThi;
    }

    public void setMaDeThi(int maDeThi) {
        this.maDeThi = maDeThi;
    }

    //Ham khoi tao rong
    public BaiThi() {
    }

    //Ham khoi tao co tham so
    public BaiThi(int maBaiThi, String deBai, String dapAn1, String dapAn2, String dapAn3, String dapAn4, int dapAnDung, int dapAnDaChon) {
        this.maBaiThi = maBaiThi;
        this.deBai = deBai;
        this.dapAn1 = dapAn1;
        this.dapAn2 = dapAn2;
        this.dapAn3 = dapAn3;
        this.dapAn4 = dapAn4;
        this.dapAnDung = dapAnDung;
        this.dapAnDaChon = dapAnDaChon;
    }

    //Ham lay danh sach bai thi
    public static List<BaiThi> layDanhSachBaiThi() {
        List<BaiThi> danhSach = new ArrayList<BaiThi>();
        String sql = "select * from bai_thi";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve result set sau khi truy van du lieu
            ResultSet rs = statement.executeQuery();
            //Lap lay tung dong du lieu
            while (rs.next()) {

                BaiThi bt = new BaiThi();
                bt.maBaiThi = rs.getInt("id");
                bt.deBai = rs.getString("de_bai");
                bt.dapAn1 = rs.getString("dap_an_1");
                bt.dapAn2 = rs.getString("dap_an_2");
                bt.dapAn3 = rs.getString("dap_an_3");
                bt.dapAn4 = rs.getString("dap_an_4");
                bt.dapAnDung = rs.getInt("dap_an_dung");
                bt.dapAnDaChon = -1;
                bt.maDeThi = rs.getInt("ma_de");
                danhSach.add(bt);

            }
            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }
        return danhSach;
    }

    //Ham lay bai thi theo de thi
    public static List<BaiThi> layBaiThiTheoMaDe(int maDe) {
        List<BaiThi> danhSach = new ArrayList<BaiThi>();
        String sql = "select * from bai_thi where ma_de=" + maDe;
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve result set sau khi truy van du lieu
            ResultSet rs = statement.executeQuery();
            //Lap lay tung dong du lieu
            while (rs.next()) {

                BaiThi bt = new BaiThi();
                bt.maBaiThi = rs.getInt("id");
                bt.deBai = rs.getString("de_bai");
                bt.dapAn1 = rs.getString("dap_an_1");
                bt.dapAn2 = rs.getString("dap_an_2");
                bt.dapAn3 = rs.getString("dap_an_3");
                bt.dapAn4 = rs.getString("dap_an_4");
                bt.dapAnDung = rs.getInt("dap_an_dung");
                bt.dapAnDaChon = -1;
                bt.maDeThi = rs.getInt("ma_de");
                danhSach.add(bt);

            }
            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }
        return danhSach;
    }

    //Ham them bai thi
    public boolean ThemBaiThi() {
        boolean rs = false;
        String sql = "insert into bai_thi(de_bai,dap_an_1,dap_an_2,dap_an_3,dap_an_4,dap_an_dung,ma_de) values (";
        sql += "'" + this.deBai + "','" + this.dapAn1 + "','" + this.dapAn2 + "','" + this.dapAn3 + "','" + this.dapAn4 + "'," + this.dapAnDung + "," + this.maDeThi;
        sql += ")";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu

            if (statement.executeUpdate() == 1) {
                rs = true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }

    //Ham cap nhat bai thi
    public boolean CapNhatBaiThi() {
        boolean rs = false;
        String sql = "update bai_thi set ";
        sql += "de_ bai='" + this.deBai + "', dap_an_1='" + this.dapAn1 + "',dap_an_2='" + this.dapAn2 + "',dap_an_3='" + this.dapAn3 + "',dap_an_4='" + this.dapAn4 + "',dap_an_dung=" + dapAnDung + ",ma_de=" + this.maDeThi;
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu

            if (statement.executeUpdate() == 1) {
                rs = true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }

    //Ham xoa bai thi
    public static boolean XoaBaiThi(int maBaiThi) {
        boolean rs = false;
        String sql = "delete from bai_thi where id=" + maBaiThi;
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu

            if (statement.executeUpdate() == 1) {
                rs = true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }
}
